package com.dys.hejiayuncommunity.common.core.exception;

import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * @className: com.dys.hejiayuncommunity.common.core.exception.GlobalExceptionHandler
 * @description: 全局异常处理器
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/02 22:51
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BaseException.class)
    public BaseResponse<String> baseExceptionHandler(BaseException ex) {
        return BaseResponse.failed(ex.getDefaultMessage());
    }

    /**
     * 自定义业务异常
     *
     * @param ex 异常对象
     */
    @ExceptionHandler(value = CustomException.class)
    public BaseResponse<String> businessException(CustomException ex) {
        if (Objects.isNull(ex.getCode())) {
            return BaseResponse.failed(ex.getMessage());
        }
        return BaseResponse.failed(ex.getCode() + "", ex.getMessage(), ex.isSuccess());
    }
}
